package com.uplink.msmq.service;

import com.alibaba.fastjson.JSONObject;
import com.uplink.msmq.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * @ Description: 数据处理
 * @ Author: hz.guo
 * @ Date: 2019-11-11 09:11
 */
@Component
@Slf4j
public class DataProcessing {
    private static final String tableName = "tableName";
    private static final String tableName2 = "tableName";

    /**
     * @ Description: 实时数据封装
     * @ Author: hz.guo
     * @ Date: 2019-11-07 15:20
     */
    public void dataProcessing(Map map) {
        List<String> list = new ArrayList<>();
        List<Map> total = new ArrayList<>();
        List<Map> data = JSONObject.parseArray(map.get("data").toString(), Map.class);
        for (int i = 0; i < data.size(); i++) {
            Map datas = new HashMap();
            Map result = new HashMap();
            result.put("i", i);
            result.put("alias", "alias0");
            result.put("dataAddress", "1");
            result.put("datatype", "a");
            result.put("devName", "b");
            result.put("devid", map.get("pathid"));
            result.put("htime", map.get("time"));
            result.put("itemid", map.get("pathid"));
            result.put("itemname", data.get(i).get("desc"));
            result.put("quality", "g");
            result.put("readOnly", "false");
            result.put("specificType", 3);
            result.put("val", data.get(i).get("value"));
            list.add(JSONObject.toJSONString(result));
            datas.put("key", result.get("devid") + "-" + map.get("hz"));
            datas.put("value", list.toString());
            total.add(datas);
        }
        writeRedisData(total, tableName);
        // 合并数据
        String pathId = map.get("pathid").toString();
        Map<String, Object> listMap = new HashMap();
        Map entries = redisTemplate.opsForHash().entries(tableName);
        List<Map> mapList = new ArrayList<>();
        for (Object key : entries.keySet()) {
            if (key.toString().split("-")[0].equals(pathId)) {
                List<Map> maps = JsonUtil.jsonToList(entries.get(key).toString());
                maps.forEach(map1 -> mapList.add(map1));
            }
        }
        listMap.put("key", pathId);
        listMap.put("value", JSONObject.toJSONString(mapList));
        List<Map> maps = Arrays.asList(listMap);
        writeRedisData(maps, tableName2);
        log.info("REDIS 写入redis成功");
        Map entries1 = redisTemplate.opsForHash().entries(tableName2);
        dataParsing(entries1, pathId);
    }

    /**
     * @ Description: 数据解析
     * @ Author: hz.guo
     * @ Date: 2019-11-08 17:14
     */
    public List<Map> dataParsing(Map entries, String key) {
        Object o = entries.get(key);
        List<Map> mapList = JSONObject.parseArray(o.toString(), Map.class);
        for (Map plcItemDataDTO : mapList) {
            //System.out.println(plcItemDataDTO);
        }
        return mapList;
    }

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * @ Description: 数据写入redis
     * @ Author: hz.guo
     * @ Date: 2019-11-07 17:10
     */
    public void writeRedisData(List<Map> list, String tableName) {
        for (Map map : list) {
            redisTemplate.opsForHash().put(tableName, map.get("key"), map.get("value"));
        }
    }
}
